package Request;

import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import servlet.RequestProcess;

import RequestHandler.*;

public class Request {    
    public final String page_index  = "/index.jsp";           
    public final String page_register  = "/register.jsp";        
    public final String page_reregister  = "/reregister.jsp";      
    public final String page_login  = "/login.jsp";           
    public final String page_relogin  = "/relogin.jsp";         
    public final String page_pcenter  = "/pcenter.jsp";         
    public final String page_search  = "/search.jsp";          
    public final String page_searchresult  = "/searchresult.jsp";    
    public final String page_itemdetail  = "/itemdetail.jsp";      
    public final String page_sell  = "/sell.jsp";            
    public final String page_selluploadresult = "/selluploadresult.jsp";
    public final String page_buyoutresult = "/buyoutresult.jsp";
    
    private RequestType requestType;
    protected int userID;
    protected int sessionID;
    private HttpServletRequest req; 
    private HttpServletResponse resp;
    private RequestDispatcher rDispatcher;
    private RequestProcess rp;
    private String nextPage;

    public void init(int uID, int sID) {
        userID = uID;
        sessionID = sID;
    }
    
    public void setupResponse(RequestProcess rp, HttpServletRequest req, HttpServletResponse resp, String nextPage) {
        this.req = req;
        this.resp = resp;
        this.rp = rp;
        this.nextPage = nextPage;
    }
    
    public void setRequestType(RequestType rt) {
        requestType = rt;
    }

    public void execute(Worker worker) {
        worker.action(this);
    }
    
    public int getUid() {
        return userID;
    }
    
    public int getSid() {
        return sessionID;
    }
    
    public void setAttribute(String name, Object value) {
        req.setAttribute(name, value);
    }
    
    public void setSession(int uid, int sid) {
        req.getSession().setAttribute("uid", uid);
        req.getSession().setAttribute("sid", sid);
    }
    
    public void setSessionAttribute(String name, Object value) {
        req.getSession().setAttribute(name, value);
    }
    
    public void reply() {
        try {
            rDispatcher = req.getRequestDispatcher(nextPage);
            System.out.println("before forward");
            rDispatcher.forward(req,resp);
            System.out.println("before notify");
            synchronized (rp) {
                rp.notify();
            }    
            System.out.println("request completed");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    public void setNextPage(String page) {
        nextPage = page;
    }
    
    public RequestType getRequestType() {
        return requestType;
    }
}